package com.treasure.infrastructure.domain.extraction.elastic;

import com.treasure.domain.domain.RelatedTextExtractionDomainService;
import com.treasure.domain.domain.entity.SegmentDocEntity;
import com.treasure.common.types.Question;
import com.treasure.infrastructure.model.doc.SegmentESDocBean;
import com.treasure.infrastructure.repository.extraction.elastic.SegmentESDocRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author sunhongmin
 * @date 2023/8/27 10:03
 * @description
 */
@Service
public class RelatedTextExtractionESDomainServiceImpl implements RelatedTextExtractionDomainService {
    @Autowired
    private SegmentESDocRepository segmentESDocRepository;

    @Override
    public List<SegmentDocEntity> relatedTextExtraction(Question question) {
        List<SegmentESDocBean> segmentESDocBeans = segmentESDocRepository.searchSegmentDoc(question.getValue());
        if (CollectionUtils.isEmpty(segmentESDocBeans)) {
            return Collections.emptyList();
        }
        List<SegmentDocEntity> result = new ArrayList<>(segmentESDocBeans.size());
        for (SegmentESDocBean segmentESDocBean : segmentESDocBeans) {
            SegmentDocEntity segmentDocEntity = new SegmentDocEntity();
            segmentDocEntity.setDocName(segmentESDocBean.getDocName());
            segmentDocEntity.setPassages(segmentESDocBean.getPassages());
            result.add(segmentDocEntity);
        }
        return result;
    }
}
